package com.instagram.instadownloader.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.os.Environment;

public class InstaFileManager {

	
	public static void getFolder(){
		Environment.getExternalStorageDirectory().getAbsolutePath();
	}
	public static final String INSTA_DOWNLOADER_FOLDER = Environment.getExternalStorageDirectory().getAbsolutePath()+"/instagramDownloader/";
	/**
	 * create new folder in sdcard follow file path
	 * @param Path path to folder that create
	 * @param name folder`s name
	 * @return File which to folder path
	 */
	public static File createNewFolder(File Path,String name) {
		File NewPath = null;
    	if(!name.trim().equals("")) {
    		NewPath=new File(Path, name);
    	if(!NewPath.exists())
    		NewPath.mkdirs();
    	}
    	
    	return NewPath;
    }
    
    public static void RenameFile(File oldFile,File newFile) {
    	if(oldFile.exists()) {
    		oldFile.renameTo(newFile);
    	}
    }
    
    /**
     * @param path to folder want to delete
     * @return true if delete sucess or false if fail
     */
    public static boolean deleteDirectory(File path) {
	    if( path.exists() ) {
	      File[] files = path.listFiles();
	      if (files == null) {
	          return true;
	      }
	      for(int i=0; i<files.length; i++) {
	         if(files[i].isDirectory()) {
	           deleteDirectory(files[i]);
	         }
	         else {
	           files[i].delete();
	         }
	      }
	    }
	    return( path.delete() );
	  }
    
    /**
     * @param source folder`s source need copy
     * @param dest folder`s dest
     * @return true if sucess
     */
    public static boolean copyFile(File source, File dest) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        
        try {
          bis = new BufferedInputStream(new FileInputStream(source));
          bos = new BufferedOutputStream(new FileOutputStream(dest, false));
          
          byte[] buf = new byte[1024];
          bis.read(buf);
          
          do {
            bos.write(buf);
          } while(bis.read(buf) != -1);
        } catch (IOException e) {
          return false;
        } finally {
          try {
            if (bis != null) bis.close();
            if (bos != null) bos.close();
          } catch (IOException e) {
            return false;
          }
        }
        
        return true;
      }
      
      // WARNING ! Inefficient if source and dest are on the same filesystem !
      public static boolean moveFile(File source, File dest) {
        return copyFile(source, dest) && source.delete();
      }
      
      public static void copyDirectory(File sourceLocation, File targetLocation)
    	      throws IOException {

    	    if (sourceLocation.isDirectory()) {
    	      if (!targetLocation.exists()) {
    	        targetLocation.mkdirs();
    	      }

    	      String[] children = sourceLocation.list();
    	      for (int i = 0; i < children.length; i++) {
    	        copyDirectory(new File(sourceLocation, children[i]), new File(
    	            targetLocation, children[i]));
    	      }
    	    } else {

//    	      copyFile(sourceLocation, targetLocation);
    	    	moveFile(sourceLocation, targetLocation);
    	    }
    	  }
      
      // Returns true if the sdcard is mounted rw
      public static boolean isSDMounted() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
      }
}
